<?php

namespace App\Jobs;

use App\Libraries\Scheduler;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Libraries\Task;
use Illuminate\Support\Facades\Log;

class TaskJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $scheduler;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Scheduler &$scheduler)
    {
        //
        $this->scheduler=clone $scheduler;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        Log::info('123456');
        $task1 = new Task(call_user_func(function() {
            for ($i = 0; $i < 3; $i++) {
                //  sleep(1);
                print "task1: " . $i . "\n";
           //     sleep(1);
                yield;
            }
        }));
        $this->scheduler->enqueue($task1);
        Log::info($this->scheduler->getQueue()->count());
    }
}
